package edu.ccut.titan.tag.chart.render;

import java.awt.Font;
import java.text.NumberFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.plot.MultiplePiePlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.Plot;
import org.jfree.util.TableOrder;

import edu.ccut.titan.tag.chart.utils.ChartInfo;

/**
 * 多饼状图渲染
 * 
 * @author LeslieGu
 *
 */
public class MultiplePieChartRender extends ChartRender {

	@Override
	protected JFreeChart createChart(ChartInfo chartInfo) {
		JFreeChart chart = ChartFactory.createMultiplePieChart(chartInfo.getTitle(),
                super.getCategoryDataset(chartInfo.getDatas()), TableOrder.BY_COLUMN,
                chartInfo.isLegend(), chartInfo.isTooltips(), chartInfo.isUrls());
		return chart;
	}
	
    protected Plot renderPrivateChart(JFreeChart chart, ChartInfo chartInfo) {

        MultiplePiePlot multiplepieplot = (MultiplePiePlot) chart.getPlot();
        JFreeChart jfreechart = multiplepieplot.getPieChart();
//        PiePlot3D pieplot = (PiePlot3D) jfreechart.getPlot();
        PiePlot pieplot = (PiePlot) jfreechart.getPlot();
        pieplot.setForegroundAlpha(0.5f);
        
      //显示百分比
        //先设置百分比的显示精度
        NumberFormat myFormat = NumberFormat.getPercentInstance();
        myFormat.setMaximumFractionDigits(2);
        //在StandardPieItemLabelGenerator中的字符串可以任意设置，其中
        //n：0—Key 1—value 2—％ 3－100
        pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} 占 {2}", myFormat, myFormat));
//        pieplot.setLabelGenerator(new StandardPieItemLabelGenerator());
        pieplot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}"));
        pieplot.setToolTipGenerator(new StandardPieToolTipGenerator("{0} 值为 {1}"));
        pieplot.setLabelFont(new Font("SansSerif", 0, 8));
        pieplot.setInteriorGap(0.29999999999999999D);

        return multiplepieplot;
    }
}
